/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */
package com.pekall.myDictionary.core;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;

import com.pekall.myDictionary.screens.ExplainScreen.Example;
import com.pekall.myDictionary.screens.ExplainScreen.Word;

public class DictCnDictSource extends DictSource {
	private final static String prefixUrl = "http://api.dict.cn/ws.php?utf8=true&q=%s";
	private final static String mSource = "Dict.cn";

	public DictCnDictSource(Context context) {
		super(context);
	}

	@Override
	public String getUrl(String[] code) {
		String query = code[0];
		try {
			String url = String.format(prefixUrl, URLEncoder.encode(query,
					"utf-8"));
			return url;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Object parseContent(String content) {
		try {
			InputStream stream = new ByteArrayInputStream(content.getBytes("utf-8"));
			InputSource source = new InputSource(stream);
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			DictCnHandler gwh = new DictCnHandler();
			xr.setContentHandler(gwh);
			xr.parse(source);
			Word set = gwh.getWord();
			if (set != null) {
				set.source = mSource;
				return set;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	public class DictCnHandler extends DefaultHandler {
		private Word mWord = null;
		private int currentstate = -1;
		private static final int KEY_STATE = 0;
		private static final int AUDIO_STATE = 1;
		private static final int PRON_STATE = 2;
		private static final int DEF_STATE = 3;
		private static final int SENT_STATE = 4;
		private static final int ORIG_STATE = 5;
		private static final int TRAINS_STATE = 6;
		private List<Example> mExamples = new ArrayList<Example>();
		private String orgi = "";
		private String trans = "";

		public Word getWord() {
			return mWord;
		}

		@Override
		public void endDocument() throws SAXException {
			super.endDocument();
		}

		@Override
		public void endElement(String uri, String localName, String name)
				throws SAXException {
			if (localName.equals("sent")) {
				Example example = new Example();
				example.orig = orgi;
				example.trans = trans;
				mExamples.add(example);
				orgi = "";
				trans = "";
			} else if (localName.equals("pron")) {
				currentstate = -1;
			} else if (localName.equals("def")) {
				currentstate = -1;
			} else if (localName.equals("sent")) {
				currentstate = -1;
			} else if (localName.equals("orig")) {
				currentstate = -1;
			} else if (localName.equals("trans")) {
				currentstate = -1;
			} else if (localName.equals("dict")) {
				mWord.examples = mExamples;
			}
		}

		@Override
		public void startDocument() throws SAXException {
			mWord = new Word();
		}

		@Override
		public void startElement(String uri, String localName, String name,
				Attributes attributes) throws SAXException {

			if (localName.equals("key")) {
				currentstate = KEY_STATE;
			} else if (localName.equals("audio")) {
				currentstate = AUDIO_STATE;
			} else if (localName.equals("pron")) {
				currentstate = PRON_STATE;
			} else if (localName.equals("def")) {
				currentstate = DEF_STATE;
			} else if (localName.equals("sent")) {
				currentstate = SENT_STATE;
			} else if (localName.equals("orig")) {
				currentstate = ORIG_STATE;
			} else if (localName.equals("trans")) {
				currentstate = TRAINS_STATE;
			}

		}

		public void characters(char ch[], int start, int length) {
			/*
			 * Would be called on the following structure:
			 * <element>characters</element>
			 */
			
			String theString = new String(ch, start, length);
			
				
				switch (currentstate) {
				case KEY_STATE:
					mWord.key = theString;
					currentstate = -1;
					break;
				case AUDIO_STATE:
					mWord.audio = theString;
					currentstate = -1;
					break;
				case PRON_STATE:
					mWord.pron += theString;
					break;
				case DEF_STATE:
					mWord.definition += theString;
					break;
				case ORIG_STATE:
					orgi += theString;
					break;
				case TRAINS_STATE:
					trans += theString;
					break;
				default:
					return;
				}
			

		}
			
	}

	@Override
	public String getSource() {
		return mSource;
	}

}
